package com.shaoshaohuo.app.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PurchaseOrder;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.ui.EvaluationActivity;
import com.shaoshaohuo.app.ui.ec.PayEcActivity;
import com.shaoshaohuo.app.ui.ec.QuoteListActivity;
import com.shaoshaohuo.app.utils.StringHelper;

import java.util.List;

public class PurchaseOrderListAdapter<T> extends MyBaseAdapter<PurchaseOrder> {

	public PurchaseOrderListAdapter(Context ctx, List<PurchaseOrder> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {

		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.view_purchase_order_item, null);
			holder = new ViewHolder();
			// holder.titleText = (TextView)
			// convertView.findViewById(R.id.textview_city_name);
			holder.titleText = (TextView) convertView.findViewById(R.id.textview_product_name);
			holder.supplierText = (TextView) convertView.findViewById(R.id.textview_gongyingshang);
			holder.phoneText = (TextView) convertView.findViewById(R.id.textview_phone);
			holder.priceText = (TextView) convertView.findViewById(R.id.textview_price);
			holder.totalText = (TextView) convertView.findViewById(R.id.textview_total);
			holder.timeText = (TextView) convertView.findViewById(R.id.textview_time);
			holder.statusText = (TextView) convertView.findViewById(R.id.textview_status);
			holder.buttonText1 = (TextView) convertView.findViewById(R.id.textview_button1);
			holder.buttonText2 = (TextView) convertView.findViewById(R.id.textview_button2);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final PurchaseOrder item = list.get(pos);

		holder.titleText.setText(item.getTitle());
		holder.supplierText.setText(item.getNum()+item.getUnit());
		holder.phoneText.setText(item.getPhone());
		holder.priceText.setText(item.getPrice()+"元/"+item.getUnit()+"*"+item.getNum()+item.getUnit());
		holder.totalText.setText("合计："+item.getTotal()+"元");
		holder.timeText.setText(StringHelper.formatDateMinute(item.getCreatetime()));
		
//		 -1买家取消订单 -2卖家取消订单，0 还没有选择供应商 1买家同意待付款 2待发货 3运输中 4已完成
		switch (Integer.parseInt(item.getStatus())) {
		case -1:
		case -2:
			holder.statusText.setText("订单关闭");
			holder.buttonText1.setVisibility(View.GONE);
            holder.buttonText2.setVisibility(View.VISIBLE);
            holder.buttonText2.setText("删除订单");
            holder.buttonText2.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                	showDeleteDialog(item);
                }
            });
			break;
		case 0:
		    holder.statusText.setText("报价中");
		    holder.buttonText1.setVisibility(View.GONE);
            holder.buttonText2.setVisibility(View.VISIBLE);
            holder.buttonText2.setText("选择供应商");
            holder.buttonText2.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,QuoteListActivity.class);
                    intent.putExtra(ExtraName.id, item.getOrderid());
                    context.startActivity(intent);
                }
            });
		case 1:
			holder.statusText.setText("待付款");
			holder.buttonText1.setVisibility(View.VISIBLE);
			holder.buttonText2.setVisibility(View.VISIBLE);
            holder.buttonText1.setText("放弃购买");
            holder.buttonText2.setText("付款");
            holder.buttonText1.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    showGiveUpBuyDialog(item);
                }
            });
            holder.buttonText2.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,PayEcActivity.class);
                    intent.putExtra(ExtraName.orderid, item.getOrderid());
                    intent.putExtra(ExtraName.totalPrice, Double.parseDouble(item.getTotal()));
                    intent.putExtra(ExtraName.action, PayEcActivity.PAY_ACTION_SUPPLY);
                    context.startActivity(intent);
                }
            });
			break;
		case 2:
			holder.statusText.setText("已付款");
			holder.buttonText1.setVisibility(View.GONE);
            holder.buttonText2.setVisibility(View.VISIBLE);
            holder.buttonText2.setText("放弃购买");
            holder.buttonText2.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    showGiveUpBuyDialog(item);
                }
            });
			break;
		case 3:
			holder.statusText.setText("货物运输中");
			holder.buttonText1.setVisibility(View.GONE);
            holder.buttonText2.setVisibility(View.VISIBLE);
            holder.buttonText2.setText("确认收货");
            holder.buttonText2.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    showReceiverDialog(item);
                }
            });
			break;
		case 4:
			holder.statusText.setText("交易完成");
            
            if ("1".equals(item.getBuydisscuz())) {
                holder.buttonText1.setVisibility(View.GONE);
                holder.buttonText2.setVisibility(View.VISIBLE);
                holder.buttonText2.setText("删除订单");
                holder.buttonText2.setOnClickListener( new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        showDeleteDialog(item);
                    }
                });
            } else {
                holder.buttonText1.setVisibility(View.VISIBLE);
                holder.buttonText2.setVisibility(View.VISIBLE);
                holder.buttonText1.setText("评价");
                holder.buttonText1.setOnClickListener( new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, EvaluationActivity.class);
                        intent.putExtra(ExtraName.type, EvaluationActivity.EVALUATION_TYPE_PURCHASE);
                        intent.putExtra(ExtraName.orderid, item.getOrderid());
                        context.startActivity(intent);
                    }
                });
                holder.buttonText2.setText("删除订单");
                holder.buttonText2.setOnClickListener( new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        showDeleteDialog(item);
                    }
                });
            }
            
			break;
		}
		
//		switch (currentStatus) {
//		case SupplyOrderActivity.STATUS_DAIFUKUAN:
//			
//			break;
//		case SupplyOrderActivity.STATUS_DAIFAHUO:
//			
//			break;
//		case SupplyOrderActivity.STATUS_DAISHOUHUO:
//			
//			break;
//		case SupplyOrderActivity.STATUS_FINISH:
//			
//			break;
//		}
		
		return convertView;
	}
	private void showDeleteDialog(final PurchaseOrder item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("确认删除订单?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                deleteOrder(item);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.create().show();
    }
	protected void deleteOrder(final PurchaseOrder item) {
	    ((BaseActivity)context).startLoadingDialog();
	    RequestService.getInstance().deletePurchaseOrder(context, item.getOrderid(), BaseEntity.class, new RequestListener() {
                
                @Override
                public void onSuccess(int requestCode, BaseEntity resultData) {
                    ((BaseActivity)context).dismissLoadingDialog();
                    if(resultData.isOk()){
                        ((BaseActivity)context).showToast("订单删除成功");
                        list.remove(item);
                        notifyDataSetChanged();
                    }else{
                        ((BaseActivity)context).showToast(resultData.getMsg());
                    }
                }
                
                @Override
                public void onFailed(int requestCode, Exception error, String msg) {
                    ((BaseActivity)context).dismissLoadingDialog();
                    ((BaseActivity)context).showToast(R.string.please_check_network);
                }
            });
    }

    protected void receiverGoods(final PurchaseOrder item) {
	    ((BaseActivity)context).startLoadingDialog();
	    RequestService.getInstance().receivePurchaseOrder(context, item.getOrderid(), BaseEntity.class, new RequestListener() {
                
                @Override
                public void onSuccess(int requestCode, BaseEntity resultData) {
                    ((BaseActivity)context).dismissLoadingDialog();
                    if(resultData.isOk()){
                        ((BaseActivity)context).showToast("确认收货成功");
                        list.remove(item);
                        notifyDataSetChanged();
                    }else{
                        ((BaseActivity)context).showToast(resultData.getMsg());
                    }
                }
                
                @Override
                public void onFailed(int requestCode, Exception error, String msg) {
                    ((BaseActivity)context).dismissLoadingDialog();
                    ((BaseActivity)context).showToast(R.string.please_check_network);
                }
            });
    }

    
    private void showReceiverDialog(final PurchaseOrder item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("确认货物已收到?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                receiverGoods(item);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.create().show();
    }
    
    private void showGiveUpBuyDialog(final PurchaseOrder item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("确认放弃?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                giveUpBuy(item);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.create().show();
    }
    
    protected void giveUpBuy(final PurchaseOrder item) {
	        ((BaseActivity)context).startLoadingDialog();
	        RequestService.getInstance().cancelPurchaseOrder(context, item.getOrderid(), BaseEntity.class, new RequestListener() {
	            
	            @Override
	            public void onSuccess(int requestCode, BaseEntity resultData) {
	                ((BaseActivity)context).dismissLoadingDialog();
	                if(resultData.isOk()){
	                    ((BaseActivity)context).showToast("取消报价成功");
	                    list.remove(item);
	                    notifyDataSetChanged();
	                }else{
	                    ((BaseActivity)context).showToast(resultData.getMsg());
	                }
	            }
	            
	            @Override
	            public void onFailed(int requestCode, Exception error, String msg) {
	                ((BaseActivity)context).dismissLoadingDialog();
	                ((BaseActivity)context).showToast(R.string.please_check_network);
	            }
	        });
    }

	/**
	 * 删除订单
	 */
	private OnClickListener mDeleteOrderListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
		}
	};
	
	static class ViewHolder {
		TextView titleText;
		TextView supplierText;
		TextView phoneText;
		TextView priceText;
		TextView totalText;
		TextView timeText;
		TextView statusText;

		TextView buttonText1;
		TextView buttonText2;
	}

}
